
$(document).ready(function() {

        //log('editAttributes.js: document.ready')

        /*
        * Ukoliko uredjujemo vlasnika, kroz ucitani html dosupne su promjenjljive:
        * 
        * var company_person;
        * var tax_type;
        * var citizenship;
        * var sex;
        * var language;
        * var country;
        * 
        */    

        var formToSubmit = '#editOwnerDialog_form';
        var formbutton = '#b-partner';

        $('[name="company_person"]').live('change',function(){
                select_owner_type(this.value);
        });

        //fill form with some data
        $('#fill_form').live('click',function(){

                log('fill')

                // General
                $("#company_person-person").attr('checked','checked');
                $("#owner_customer").attr('checked','checked');
                $("#owner_suplier").attr('checked','checked');
                $("#name").val('ivan');
                $("#surname").val('kukic');
                $("#personal_ID").val('123456');
                $("#tax_type").val('Local tax');
                $("#birthdate").val('01.08.1982');                
                $("#birthplace").val('Banjaluka');
                $("#pasport_number").val('AC37B6V');
                $("#citizenship").val('montenegrin');
                $("#sex").val('male');
                $("#language").val('Montenegrin');

                // Contact data
                $("#address").val('Jovana Tomaševića');
                $("#city").val('Bar');
                $("#zip_code").val('85000');
                $("#country").val('Montenegro');
                $("#email").val('ikukic@yahoo.com');
                $("#mobile_phone").val('069 069 982');
                $("#telephone").val('312 324');
                $("#telephone2").val('312 324');
                $("#telefax").val('312 324');

                // Bank
                $("#bank_name").val('NLB Montenegro');
                $("#bank_ac_number").val('530-20322-60');
                $("#swift").val('u38s00sj');

                // Other details
                $("#contract_type").val('1 year');
                $("#default_option_period").val('123');
                $("#accounting_code").val('321');
                $("#contract_nr").val('001-12');
                $("#due_date").val('432');

        })


        if(typeof(postAction)==='undefined')
            {

        }else{
            switch (postAction) {
                case 'NEW':
                    select_owner_type('company');
                    $("#tax_type").val(0);
                    $("#citizenship").val(0);
                    $("#sex").val(0);
                    $("#language").val(0);
                    $("#country").val(0);
                    break;
                case 'EDIT':
                    myOwner[0]['type'] = company_person;
                    select_owner_type(myOwner[0]['type']);
                    $("#tax_type").val(tax_type);
                    $("#citizenship").val(citizenship);
                    $("#sex").val(sex);
                    $("#language").val(language);
                    $("#country").val(country);
                    break;
                default:
                    alert('myOwner javascript object mode is undefined');
            }   
        }

        //FORM VALIDATION
        jQuery(formToSubmit).validate({
                /*rules: {
                'owner_type[]': {required :true},                     
                surname: "required",                                                               
                tax_type: { check_item_dropdown: true }
                }, */ 
                highlight: function(element) {
                    console.log(element.name)
                    if(element.id=='email' || element.name=='company_person' || element.name=='owner_type[]'){
                        $(element).parent().parent().parent().addClass("error");
                    }else{
                        $(element).parent().parent().addClass("error");
                    }
                },
                unhighlight: function(element) {
                    if(element.id=='email' || element.name=='company_person' || element.name=='owner_type[]'){
                        $(element).parent().parent().parent().removeClass("error");
                    }else{
                        $(element).parent().parent().removeClass("error");
                    }
                },
                submitHandler: function(form) {

                    //disable add button                            
                    $(formbutton).attr("disabled","disabled");

                    $(formToSubmit+' .status_info').html(loader_html());

                    var form = $(formToSubmit);
                    var form_data = form.serialize();

                    $.ajax({
                            url: form.attr('action'),
                            dataType: 'json',
                            type: "POST",
                            data: form_data,
                            success: function (data, textStatus, xhr) {

                                if(data.action==true){

                                    $(formToSubmit+' .status_info').html(alert_html(data.msg,'success')); 

                                    function action_delay(){                                                
                                        window.location.href = base_url+'partners/view_all'; 
                                    }
                                    window.setTimeout(function() { action_delay(); }, 1001);

                                }else{

                                    $(formbutton).removeAttr("disabled");
                                    $(formToSubmit+' .status_info').html(alert_html(data.msg,'error'));
                                    $(formToSubmit).animate({
                                            scrollTop:  $(formToSubmit+' .status_info').offset().top
                                        }, 200);
                                }
                            },
                            error: function (xhr, textStatus, errorThrown) {
                                //enable add button                            
                                $(formbutton).removeAttr("disabled");
                                $(formToSubmit+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                            }
                    });
                },
                errorPlacement: function(error, element){}
        })

        $(formbutton).removeAttr("disabled");

})